{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "255f4582",
   "metadata": {},
   "source": [
    "## 本周知识点\n",
    "### 1、字典的初始（{key:value}）\n",
    ">1、字典的文档\n",
    "\n",
    ">2、字典的重要属性：\n",
    " >> * 1、结构：key:value（键值对）\n",
    " >> * 2、字典是无序的，我们通过键（key）去查找值（value），就好像list用索引（index）去查找值（value）一样，只不过字典“索引”被定义了名字\n",
    " >> * 3、字典对key的定义有一定的要求：key是由“不可变”的数据结构才可以被定义，list是不能作为key来定义的。\n",
    " >> * 4、字典对key是唯一的（list的值不一定是唯一的），字典的value也可以不唯一。\n",
    "\n",
    ">3、字典的创建"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "fcbcf9ae",
   "metadata": {},
   "outputs": [],
   "source": [
    "users=[\n",
    "    [\"aaa\",12,\"student\",100],\n",
    "    [\"bbb\",34,\"teacher\",5000]\n",
    "]\n",
    "#试对比:\n",
    "users_dict = {\n",
    "    \"aaa\":[12,\"student\",100],\n",
    "    \"bbb\":[34,\"teacher\",5000]\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "1cd0f547",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['bbb', 34, 'teacher', 5000]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#查询aaa的信息：\n",
    "users[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "a7b877a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[34, 'teacher', 5000]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "users_dict[\"bbb\"]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "dfb8b107",
   "metadata": {},
   "source": [
    "* 我要查找其中的一个信息，但无法的知道其索引值\n",
    "* list就变得很无力\n",
    "* 而dict只需要知道数据的关键信息即可"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "79eb5e72",
   "metadata": {},
   "source": [
    "### 2、字典的创建（常见的三种创建方式）\n",
    ">1、info={key1：value1，key：value2,...}直接按照字典格式创建\n",
    "\n",
    ">2、info_02=dict(key1=value1,key2=value2,...)使用dict()进行字典创建\n",
    "\n",
    ">3、info_03={}info_03[key1]=value1,info_03[key2]=value2     用字典增加的方式新建字典"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "615d407e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1001': 'aaa', 'stu1002': 'bbb', 'stu1003': 'ccc', 'stu1004': 'aaa'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info={\n",
    "    \"stu1001\":\"aaa\",\n",
    "    \"stu1002\":\"bbb\",\n",
    "    \"stu1003\":\"ccc\",\n",
    "    \"stu1004\":\"aaa\"\n",
    "}\n",
    "print(info)\n",
    "print(type(info))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "15921f0f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1001': 'aaa', 'stu1002': 'bbb', 'stu1003': 'ccc'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_02=dict(\n",
    "    stu1001=\"aaa\",\n",
    "    stu1002=\"bbb\",\n",
    "    stu1003=\"ccc\",\n",
    ")\n",
    "print(info_02)\n",
    "print(type(info_02))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "70f8bf0e",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1001': 'aaa', 'stu1002': 'bbb', 'stu1003': 'ccc'}\n",
      "<class 'dict'>\n"
     ]
    }
   ],
   "source": [
    "info_03={}#新建空字典\n",
    "info_03[\"stu1001\"]=\"aaa\"\n",
    "info_03[\"stu1002\"]=\"bbb\"\n",
    "info_03[\"stu1003\"]=\"ccc\"\n",
    "print(info_03)\n",
    "print(type(info_03))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75b18dee",
   "metadata": {},
   "source": [
    "### 字典的增、删、改、查\n",
    "\n",
    ">1、字典的增加（和字典的第三种创建方法一致）：\n",
    ">> * info_03[\"stu1005\"]=\"ddd\"\n",
    "\n",
    ">2、字典的查询\n",
    ">> * info_03[key_name]\n",
    ">> * 注意：正确的查询方式，应该先查询key是否存在字典当中，如下：\n",
    ">>>1.key_name in info_03  ##查看是否存在，如果存在，返回结果为 ture,不存在为false；\n",
    ">>>2.info_03[key_name]\n",
    "\n",
    ">3.字典的修改\n",
    ">>* 查询key是否存在\n",
    ">>* 修改key的值，例如：\n",
    ">>>修改info_03中stu1003的值为fff,if\"stu1003\"in info_03:info_03[\"stu1003\"]=\"fff\"\n",
    "\n",
    ">4.字典的删除\n",
    ">>* pop(key)指定值删除\n",
    ">>* popitem()随机值删除\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "4164e761",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1001': 'aaa', 'stu1002': 'bbb', 'stu1003': 'ccc', 'stu1005': 'ddd'}\n"
     ]
    }
   ],
   "source": [
    "info_03[\"stu1005\"]=\"ddd\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "0e91bbde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'bbb'"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#用key查到value的值\n",
    "info_03[\"stu1002\"]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "10e59c98",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n",
      "ccc\n"
     ]
    }
   ],
   "source": [
    "#正确的查询字典的方式\n",
    "print(\"stu1002\"in info_03)\n",
    "if\"stu1003\" in info_03:\n",
    "     print(info_03[\"stu1003\"])\n",
    "else:\n",
    "    print(查询的key值不存在)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "eff49e6c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1001': 'aaa', 'stu1002': 'bbb', 'stu1003': 'fff', 'stu1005': 'ddd'}\n"
     ]
    }
   ],
   "source": [
    "#修改：也要先查询key\n",
    "if \"stu1003\" in info_03:\n",
    "   info_03[\"stu1003\"]=\"fff\"\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "2f41a3a8",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1001': 'aaa', 'stu1002': 'bbb', 'stu1005': 'ddd'}\n"
     ]
    }
   ],
   "source": [
    "#删除：\n",
    "info_03.pop(\"stu1003\")\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "373c647a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'stu1001': 'aaa', 'stu1002': 'bbb'}\n"
     ]
    }
   ],
   "source": [
    "#字典的删除 popitem()\n",
    "info_03.popitem()\n",
    "print(info_03)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a156e18",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
